﻿using System;
using System.Collections.Generic;

namespace _03.AnimalHierarchy
{
    public abstract class Animal : ISound
    {
        #region Fields
        
        protected int age;
        
        protected string name;
        
        protected string sex;
        
        #endregion

        #region Constructors

        public Animal(int age, string name, string sex)
        {
            this.age = age;
            this.name = name;
            this.sex = sex;
        }

        #endregion

        #region Properties

        public string Sex
        {
            get
            {
                return this.sex;
            }
            set
            {
                if (value != "female" || value != "male")
                {
                    throw new ArgumentOutOfRangeException("The sex of the animal can only be female or male");
                }
                this.sex = value;
            }
        }
        
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ArgumentNullException("The name cannot be null");
                }
                if (value.Length < 2)
                {
                    throw new ArgumentOutOfRangeException("The length of the name cannot be less then 2");
                }
                
                this.name = value;
            }
        }
        
        public int Age
        {
            get
            {
                return this.age;
            }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentOutOfRangeException("The age cannot be negative");
                }
                this.age = value;
            }
        }

        #endregion 

        #region Methods

        public abstract void ProduceSound();
        
        public static double Average(List<Animal> animalList)
        {
            double average = 0;
            for (int index = 0; index < animalList.Count; index++)
            {
                average += animalList[index].Age;
            }
            
            average /= animalList.Count;
            return average;
        }

        #endregion
    }
}